﻿// <copyright file="ExportForm.XML.cs" company="BerlinSoft">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>Wernfried Schwenkner</author>
// <email>wernfried@schwenkner.de</email>
// <date>2009-10-21</date>
// <summary>
// Contains the ExportForm for XML export.
// </summary>

using System;
using System.Collections.Generic;
using System.Data.SqlServerCe;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using SenseTimeTracker.Misc;


namespace SenseTimeTracker.Export
{
	/// <summary>
	/// Class implements the export form for XML
	/// </summary>
	public partial class ExportForm
	{
		/// <summary>
		/// Exports cards to XML
		/// </summary>
		/// <param name="date">Date within the given period to export</param>
		/// <param name="exportPeriod">Period to be exported</param>
		public void ExportToXML(DateTime date, ExportPeriod exportPeriod)
		{
			DateTime dateBegin;
			DateTime dateFinal;
			TimeTrackerXML timeCards = GetTimeCards(date, exportPeriod, out dateBegin, out dateFinal);
			timeCards.Serialize(DetermineFilename(dateBegin, dateFinal, "xml", exportPeriod == ExportPeriod.Week));
		}

		/// <summary>
		/// Gets the time cards within the given period
		/// </summary>
		/// <param name="date">Date within the given period</param>
		/// <param name="exportPeriod">Period to be exported</param>
		/// <param name="dateBegin">Begin date</param>
		/// <param name="dateFinal">Final date</param>
		/// <returns>The records to export in XML notation</returns>
		private TimeTrackerXML GetTimeCards(DateTime date, ExportPeriod exportPeriod, out DateTime dateBegin, out DateTime dateFinal)
		{
			switch (exportPeriod)
			{
				case ExportPeriod.Week:
					dateBegin = DateOfMonday(date);
					dateFinal = dateBegin.AddDays(7).Date;
					break;
				case ExportPeriod.Month:
					dateBegin = new DateTime(date.Year, date.Month, 1);
					dateFinal = new DateTime(date.Year, date.Month + 1, 1);
					break;
				default:
					throw new Exception("Unsupported period!");
			}

			DatabaseManager.DbConnection.Open();
			SqlCeDataReader reader = GetTimeCards(dateBegin, dateFinal, DatabaseManager.DbConnection);
			TimeTrackerXML timeCards = null;
			TimeTrackerXML.Client client = null;
			TimeTrackerXML.Project project = null;
			TimeTrackerXML.Task task = null;

			while (reader.Read())
			{
				if (timeCards == null)
				{
					timeCards = new TimeTrackerXML();
				}

				if (client == null || client.Id != (Guid)reader["clientId"])
				{
					client = new TimeTrackerXML.Client()
					{
						Id = (Guid)reader["clientId"],
						Name = reader["clientName"].ToString(),
						Type = reader["clientType"].ToString(),
					};

					timeCards.Clients.Add(client);
				}

				if (project == null || project.Id != (Guid)reader["projectId"])
				{
					project = new TimeTrackerXML.Project()
					{
						Id = (Guid)reader["projectId"],
						Name = reader["projectName"].ToString(),
						Number = reader["projectNumber"].ToString(),
						Type = reader["projectType"].ToString(),
					};

					client.Projects.Add(project);
				}

				if (task == null || task.Id != (Guid)reader["taskId"])
				{
					task = new TimeTrackerXML.Task()
					{
						Id = (Guid)reader["taskId"],
						Name = reader["taskName"].ToString(),
						Number = reader["taskNumber"].ToString(),
						Type = reader["taskType"].ToString(),
						Location = reader["taskLocation"].ToString(),
						Description = reader["taskDescription"].ToString(),
						Estimate = (int)reader["taskEstimate"],
					};

					project.Tasks.Add(task);
				}

				task.TimeCards.Add(new TimeTrackerXML.TimeCard()
				{
					Description = reader["description"].ToString(),
					Details = reader["details"].ToString(),
					StartTime = (DateTime)reader["timeRecordStartTime"],
					FinalTime = (DateTime)reader["timeRecordFinalTime"],
				});
			}

			DatabaseManager.DbConnection.Close();
			return timeCards;
		}
	}
}
